<template>
  <div class="login-container">
      <!-- 导航栏 -->
      <van-nav-bar  title="用户登录" >
        <van-icon slot="left" name="cross"  @click="$router.back()"/>
      </van-nav-bar>
      <!-- 登陆表单 -->
      <van-form ref="loginForm" @submit="onSubmit">
        <van-field
          v-model="user.username"

          type="string"
          name="username"
          label="帐户名"
          placeholder="请输入登录帐号"
          :rules="userFormRules.mobile"
          maxlength="11"
        />
        <van-field
        v-model="user.password"
          name="password"
          label="密码"
          placeholder="请输入密码"
          :rules="userFormRules.code"
          type="number"
          maxlength="6"
        >
        <template #button>
          <!-- 倒计时
          time就是倒计时时间
          -->
          <!-- <van-count-down v-if="isCountDownShow" :time="1000 * 60" format="ss s后可重新发送" @finish="isCountDownShow=false" />
          <van-button round native-type="button" class="send-sms-btn" size="small" type="default" @click="onSendSms" v-else>发送验证码</van-button> -->
        </template>
      </van-field>
        <div class="login-btn-wrap">
          <van-button class="login-btn"  block type="info" native-type="submit">用户登录</van-button>
          <van-button class="login-btn"  block type="info" @click.prevent="$router.push('/adminLogin')">管理员登录</van-button>
        </div>
        <van-cell class="reg-btn" title="没有账号？点击注册" is-link  @click="$router.push('/reg')"/>

      </van-form>
  </div>
</template>

<script>
import { login, userReg } from '@/api/user'
export default {
  name: 'LoginIndex',
  data () {
    return {
      show: false, // 验证弹窗
      user: {
        username: 'qq', // 账户名
        password: '123456' // 密码
      },

      userFormRules: {
        mobile: [{ required: true, message: '手机号不能为空' }, {

        }],
        code: [{ required: true, message: '密码不能为空' }

        ]
      }
      // isCountDownShow: false
    }
  },
  methods: {
    async onSubmit () {
      // 1.获取表单数据
      const user = this.user
      // 2.表单验证
      // 在组件中必须通过this.$toast来调用toast
      this.$toast.loading({
        message: '登陆中...',
        forbidClick: true, // 禁用背景点击
        duration: 0 // 如果为0，则持续展示
        // loadingType: 'spinner'
      })
      // 3.提交表单请求登录
      try {
        const { data: { data: res } } = await login(user)
        console.log(res)
        if (res.username === user.username && res.password === user.password) {
          this.$toast.success('登录成功')
          // 登陆成功，跳转回原来页面
          this.$router.push('/my')
        }
        this.$store.commit('setUser', res)
      } catch (error) {
        this.$toast.fail('登陆失败,请稍后重试', error)
      }
      // 4.根据响应结果处理后续操作
    }

  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #3926fa;
 /deep/ .van-nav-bar__title {
    color: #fff;
  }
}
.send-sms-btn {
  background-color: #ededed;

}

.login-btn-wrap {
  padding: 53px 33px;

  display: flex;
  justify-content: space-around;
  .login-btn {
    background-color: #6db4fb;
    border: none;
    margin-right: 5px;
  }

}
</style>
